<?php
namespace Openapi;
set_include_path(get_include_path() . PATH_SEPARATOR. '/faas-sdk-php');
require_once 'pkg/client/client.php';
use Client\SDKClient;

// 工作流调用请求参数
class InvokeFlowRequest {
    public $workflowName;
    public $input;

    public function __construct($workflowName = '', $body = null) {
        $this->workflowName = $workflowName;
        $this->input = $body;
    }
}

// 工作流调用响应
class InvokeFlowResponse {
    public $headers;
    public $statusCode;
    public $body;

    public function __construct($headers = [], $statusCode = null, $body = null) {
        $this->headers = $headers;
        $this->statusCode = $statusCode;
        $this->body = $body;
    }
}

// 工作流服务类
class CloudFlowClient {
    private $client;

    public function __construct(SDKClient $client) {
        $this->client = $client;
    }

    public function setEndpoint($endpoint) {
        $this->endpoint = $endpoint;
    }

    private function newHeader($region) {
        return [
            'Content-Type' => 'application/json',
            'regionId' => $region
        ];
    }

    public function invokeWorkflow($region, InvokeFlowRequest $request) {
        if (!$request->workflowName) {
            return new InvokeFlowResponse([], null, ['error' => 'workflowName is required']);
        }

        $headers = $this->newHeader($region);
        $url = "/openapi/v1/execution/start";

        try {
            $config = [
                'method' => 'POST',
                'url' => $url,
                'headers' => $headers,
                'data' => $request,
            ];

            $response = $this->client->doRequest($config);
            printf('OpenAPI response with: %s', $response->data);
            return $response;
        } catch (\Exception $e) {
            return new InvokeFlowResponse([], 500, ['error' => $e->getMessage()]);
        }
    }
}

?>